package com.itheima.util;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;

public class UploadUtils {
    //阿里云域名bucket
    //public static final String bucket="https://qianziwei12138.oss-cn-hangzhou.aliyuncs.com/";

    public static String upload(MultipartFile file) throws IOException {
        String originalFilename = file.getOriginalFilename();
        int i = originalFilename.lastIndexOf(".");

        String suffix = originalFilename.substring(i);
        String fileName = UUID.randomUUID().toString()+suffix;

        //地域节点
        String endpoint="https://oss-cn-hangzhou.aliyuncs.com";

        String accessKeyId = "LTAI5t7MrWWJT73SBtEnwFVK";
        String accessKeySecret = "BDaDaofiVLCIBODQSQYGrKqteL6DO3";
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        ossClient.putObject("qianziwei12138",fileName,file.getInputStream());
        ossClient.shutdown();
        return fileName;
    }
}
